﻿using System;
using System.Collections.Generic;
using UnityEngine;
using System.Xml;

/// <summary>
/// 技能效果结构
/// </summary>
public class SkillEffectVo
{
    public int SkillID;

    //技能行为列表
    private List<SkillActionVo> m_ActionList;
    public List<SkillActionVo> ActionList
    {
        get
        {
            return m_ActionList;
        }
        set
        {
            m_ActionList = value;
        }
    }

    public SkillEffectVo()
    {
        m_ActionList = new List<SkillActionVo>();
    }

    public static SkillEffectVo Create(int SkillID)
    {
        SkillEffectVo result = new SkillEffectVo();
        result.SkillID = SkillID;
        return result;
    }

    public static SkillEffectVo Create(string xmlString)
    {
        if (string.IsNullOrEmpty(xmlString))
        {
            Debug.LogError("解析技能xml出错!");
            return null;
        }

        SkillEffectVo result = new SkillEffectVo();

        XmlDocument xml = new XmlDocument();
        xml.LoadXml(xmlString);

        try
        {
            int.TryParse(xml.DocumentElement.Attributes["SkillId"].Value, out result.SkillID);
        }
        catch (Exception e)
        {
            Debug.LogError(e.ToString());
        }

        XmlNodeList nodeList = xml.SelectNodes("root/action");

        if(nodeList != null)
        {
            foreach (XmlNode node in nodeList)
            {

                SkillActionVo actionVo = new SkillActionVo();

                try
                {
                    int temp = 0;
                    int.TryParse(node.Attributes["propId"].Value, out actionVo.propId);

                    int.TryParse(node.Attributes["type"].Value, out temp);
                    actionVo.type = (SkillActionEnum)temp;

                    float.TryParse(node.Attributes["delayTime"].Value, out actionVo.delayTime);

                    int.TryParse(node.Attributes["animation"].Value, out temp);
                    actionVo.animation = (AnimationType)temp;

                    actionVo.resourceName = node.Attributes["resourceName"].Value;

                    result.ActionList.Add(actionVo);

                }
                catch(Exception e)
                {
                    Debug.LogError("解析SkillActionVo失败" + e.ToString());
                }

            }
        }
        return result;
    }

    public void AddAction(SkillActionVo vo)
    {
        SkillActionVo v = m_ActionList.Find(xx => xx.propId == vo.propId);
        if (v == null)
        {
            m_ActionList.Add(vo);
        }
        else
        {
            int index = m_ActionList.IndexOf(v);
            m_ActionList.RemoveAt(index);
            m_ActionList.Insert(index, vo);
        }
    }

    public void RemoveAction(int propId)
    {
        SkillActionVo v = m_ActionList.Find(xx => xx.propId == propId);
        if (v != null)
        {
            m_ActionList.Remove(v);
        }
    }

    public string ToXmlString()
    {
        string xml = "<root";
        xml += @" SkillId=""" + SkillID.ToString() + @"""";

        xml += ">\n\n";

        foreach(SkillActionVo actionVo in m_ActionList)
        {
            xml += "  <action";
            xml += @" propId=""" + actionVo.propId + @"""";
            xml += @" type=""" + ((int)(actionVo.type)).ToString() + @"""";
            xml += @" delayTime=""" + actionVo.delayTime.ToString() + @"""";
            xml += @" animation=""" + ((int)actionVo.animation).ToString() + @"""";
            xml += @" resourceName=""" + actionVo.resourceName + @"""";
            xml += "></action>\n";
        }

        xml += "\n</root>";
        return xml;
    }

}

/// <summary>
/// 技能动作数据结构
/// </summary>
public class SkillActionVo
{
    //技能行为ID 
    public int propId = 0;
    //技能行为类型
    public SkillActionEnum type = SkillActionEnum.playSkillEffect;
    //延迟时间
    public float delayTime;
    //播放动作名
    public AnimationType animation = AnimationType.daiji;
    //资源
    public string resourceName;

    //创建一个技能行为数据
    public static SkillActionVo Creat(int id, SkillActionEnum type, float delayTime, AnimationType animation,string res)
    {
        SkillActionVo vo = new SkillActionVo();
        vo.propId = id;
        vo.type = type;
        vo.delayTime = delayTime;
        vo.animation = animation;
        vo.resourceName = res;
        return vo;
    }

    public void CopyFrom(SkillActionVo target)
    {
        propId = target.propId;
        type = target.type;
        delayTime = target.delayTime;
        animation = target.animation;
        resourceName = target.resourceName;
    }

    private string contentFormat = "{0};{1};{2};{3};{4}";
    public override string ToString()
    {
        return string.Format(contentFormat, propId, ((int)(type)), delayTime, ((int)animation),resourceName);
    }

}
